package service

import (
	"context"
	"github.com/spf13/cast"
	"log"
	redis "nearby/biz/dal/cache"
	"nearby/biz/dal/db/repo"
	"nearby/biz/model"
)

type ViewMomentDetailService struct {
}

func (s *ViewMomentDetailService) Execute(ctx context.Context, req *model.GetMomentDetailRequest) error {
	// 在redis的队列里存一条消息
	return redis.HIncrBy(ctx, redis.ViewMomentKey, cast.ToString(req.MomentID), 1).Err()
}

func (s *ViewMomentDetailService) SyncViewCnt(ctx context.Context, momentId int64, incr int64) error {
	momentRepo := repo.NewMomentRepo(ctx)
	momentPo, err := momentRepo.GetMoment(ctx, repo.GetMomentRequest{
		MomentID: cast.ToInt64(momentId),
	})
	if err != nil {
		log.Printf("SyncViewCnt fail, err: %+v", err)
		return err
	}
	if momentPo == nil {
		log.Printf("momentPo is nil, key is %+v, val is %+v", momentId, incr)
		return err
	}
	momentPo.ViewCount += cast.ToInt64(incr)
	return momentRepo.UpdateViewCnt(ctx, *momentPo)
}
